package com.fms.gui.templateCreator.components.date;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;

import javax.swing.JLabel;
import javax.swing.JPanel;

import com.fms.gui.templateCreator.components.date.parts.BackDrop;
import com.fms.gui.templateCreator.components.date.parts.Hand;

public class TimeSelector extends JPanel implements  MouseMotionListener, MouseListener
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 5432524284806954020L;
	private Hand hourHand;
	private Hand minuteHand;
	private BackDrop	backDrop;
	private JLabel timeLabel;
	
	public TimeSelector(IClockFactory handFactory)
	{
		this.hourHand = handFactory.getHourHand();
		this.minuteHand = handFactory.getMinuteHand();
		this.backDrop = handFactory.getBackDrop();
		this.timeLabel = handFactory.getTimeLabel();

		this.setPreferredSize(new Dimension(220,220));
		this.setMaximumSize(new Dimension(220,220));
		this.setMinimumSize(new Dimension(220,220));
		this.addMouseMotionListener(this);
		this.addMouseListener(this);
		
		this.setLayout(new BorderLayout());
		
		
		this.add(timeLabel, BorderLayout.SOUTH);
	}


	public void paint(Graphics g) 
	{
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
	  
	    double middleX = (double)this.getSize().getWidth() / 2.0;
	    double middleY = (double)this.getSize().getHeight() / 2.0;
	    
	    AffineTransform affineTransform = AffineTransform.getTranslateInstance(middleX, middleY);
	    g2d.transform(affineTransform);
	  
	    backDrop.paint(g);
	    hourHand.paint(g);
	    minuteHand.paint(g);
	    
	    
    }


	
	public void mouseDragged(MouseEvent arg0)
	{
		
		moveMinuteHand(arg0);
	}
	
	
	public void mouseClicked(MouseEvent arg0)
	{
		moveMinuteHand(arg0);
		
	}

	
	public void mouseMoved(MouseEvent arg0) {}

	private void moveMinuteHand(MouseEvent arg0)
	{
		int x = (int) (arg0.getPoint().x - this.getSize().getWidth()/2.0);
		int y = -(int) (arg0.getPoint().y - this.getSize().getHeight()/2.0);

		minuteHand.moveTo(x, y);
		this.repaint();
	}





	
	public void mouseEntered(MouseEvent arg0){}


	
	public void mouseExited(MouseEvent arg0) {}



	public void mousePressed(MouseEvent arg0) {}


	
	public void mouseReleased(MouseEvent arg0)	{}





	
}
